package com.jjwxc.jwjskandriod.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.adapter.RechargeViewHolder;
import com.jjwxc.jwjskandriod.config.BaseActivity;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.refresh.recyclerview.BaseRecyclerAdapter;
import com.jjwxc.jwjskandriod.framework.utils.FFUtils;
import com.jjwxc.jwjskandriod.framework.utils.StUtils;
import com.jjwxc.jwjskandriod.model.UserRechargeResponse;
import com.jjwxc.jwjskandriod.widget.LinearLayouDecoration;

import java.util.ArrayList;
import java.util.List;

public class RechargeActivity extends BaseActivity {
    private List<String> mList = new ArrayList<>();
    private RechargeAdapter adapter;
    private RecyclerView rv_recharge;

    @Override
    public int getContentViewId() {
        return R.layout.activity_recharge;
    }

    @Override
    public void findView() {
        setTitle("我的充值");
        rv_recharge = findViewById(R.id.rv_recharge);
        rv_recharge.setNestedScrollingEnabled(false);
        rv_recharge.setFocusable(false);
        rv_recharge.getItemAnimator().setChangeDuration(0);
        rv_recharge.setLayoutManager(new LinearLayoutManager(RechargeActivity.this));
        rv_recharge.addItemDecoration(new LinearLayouDecoration(RechargeActivity.this,
                LinearLayouDecoration.HORIZONTAL, R.dimen.border8, R.color.transparent));

    }

    @Override
    public void setListener() {
        adapter = new RechargeAdapter(this);
        rv_recharge.setAdapter(adapter);
    }

    @Override
    public void afterCreate() {
        post(Url.listUserRecharge, null, UserRechargeResponse.class, new FFNetWorkCallBack<UserRechargeResponse>() {
            @Override
            public void onSuccess(UserRechargeResponse response) {
                if (response.getCode() == 200) {
                    adapter.setData(response.getData());
                }

            }

            @Override
            public boolean onFail(UserRechargeResponse response) {
                return false;
            }
        },"{\"offset\":0,\"limit\":10}");

    }


    public class RechargeAdapter extends BaseRecyclerAdapter<RechargeViewHolder> {
        private Context context;


        private List<UserRechargeResponse.DataBean> mData = new ArrayList<>();


        public RechargeAdapter(Context cnt) {
            this.context = cnt;

        }

        public void addData(List<UserRechargeResponse.DataBean> data) {
            if (data != null) {
                mData.addAll(data);
                notifyDataSetChanged();
            }
        }

        public void setData(List<UserRechargeResponse.DataBean> data) {
            this.mData.clear();
            addData(data);
        }

        /**
         * 创建ViewHolder
         */
        @Override
        public RechargeViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {

            View mView = LayoutInflater.from(context).inflate(R.layout.recharge_item, parent, false);
            RechargeViewHolder viewHolder_text = new RechargeViewHolder(mView);
            viewHolder_text.setIsRecyclable(false);
            return viewHolder_text;

        }

        @Override
        public RechargeViewHolder getViewHolder(View view) {
            return new RechargeViewHolder(view);
        }


        /**
         * 绑定数据：可以直接拿到已经绑定控件的Viewholder对象
         */
        @SuppressLint({"SetTextI18n", "ClickableViewAccessibility"})
        @Override
        public void onBindViewHolder(final RechargeViewHolder holder, final int position, boolean isItem) {
            holder.tv_num.setText("￥ "+ StUtils.percentage(mData.get(position).getPaymentAmount()));
            holder.tv_price.setText(mData.get(position).getAmount() + "完结币");
            holder.tv_time.setText(FFUtils.stringToDates(mData.get(position).getCreateTime()+""));
        }

        @Override
        public int getAdapterItemViewType(int position) {
            return position;
        }


        @Override
        public int getAdapterItemCount() {
            return mData.size();
        }


    }
}
